clc
clear

ncfile = '../run/hope_output.n30.nc';

radius = 6371229;
D2R = pi / 180;
LineWidth = 1;

% var_name  = 'us';
% cmin = 18;
% cmax = 22;
% var_name  = 'vs';
% cmin = -1;
% cmax = 1;
% var_name  = 'w';
% cmin = -1;
% cmax = 1;
% LevelStep = 0.05;
var_name  = 'p';
cmin = 0;
cmax = 100000;
LevelStep = 5000;
% var_name  = 'rho';
% cmin = 0;
% cmax = 1.4;
% var_name  = 't';
% cmin = 180;
% cmax = 300;
% var_name  = 'theta';
% cmin = 200;
% cmax = 1200;

j         = 15;
it        = 24;
FontSize  = 10;

lon = ncread(ncfile,'lon');
lat = ncread(ncfile,'lat');
p   = ncread(ncfile,'p'  ,[1,1,1,1,it],[Inf,Inf,Inf,Inf,1]);
z   = ncread(ncfile,'z');
var = ncread(ncfile,var_name,[1,1,1,1,it],[Inf,Inf,Inf,Inf,1]);
dx  = ncreadatt(ncfile,'/','dx');
dx  = dx * D2R * radius;

nx  = size(lon(:,1,1),1);
nz  = size(z,4);

for idom = 1:4
    is = (idom-1)*nx+1;
    ie = idom*nx;
    ks = 1;
    ke = nz;
    % i case
    lon_plt(is:ie,ks:ke) = repmat(squeeze(lon(:,j,idom)),1,nz);
    
    if idom==1
        lon_plt(1:floor(nx/2),:) = lon_plt(1:floor(nx/2),:) - 360;
    end
    
    z_plt(is:ie,ks:ke) = squeeze(z(1,:,j,:,idom));
    
    if strcmp(var_name,'z')
        var_plt(is:ie,ks:ke) = squeeze(var(1,:,j,:,idom));
    else
        var_plt(is:ie,ks:ke) = squeeze(var(:,j,:,idom));
    end
    
    for i = 1:nx
        x_plt(is+i-1,1:nz) = ( is + i - 1 ) * dx;
    end
end

figure
plt = pcolor(x_plt,z_plt,var_plt);
set(gca,'Clim',[cmin,cmax])
% set(plt,'EdgeColor','None')
% shading interp
colormap(jet)
% colorbar
hold on
[plt,h] = contour(x_plt,z_plt,var_plt,'k--','LevelStep',LevelStep,'LineWidth',LineWidth);
set(gca,'Clim',[cmin,cmax])
colormap(jet)
clabel(plt,h,'FontSize',FontSize)
% colorbar
title([var_name,' at ',num2str(it)])
